package com.fireline.beauty.member;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.ControlUtil;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.IDataBaseProvide;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 会员交费（储值卡充值、套餐续费）追收欠款窗口
 * */
public class HyjfhkDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	/**
	 * @wbp.nonvisual location=57,482
	 */
	private final Dataset dataset = new Dataset(
			"select a.*,b.hyxm from hyjf a \r\nleft join hyxx b on a.hybh=b.hybh\r\nwhere id=?",
			SqliteJdbcImpl.INSTANCE);
	private CLabel lblNewLabel;
	private CLabel label_2;
	private CLabel label_5;
	private CLabel label_6;
	private CLabel label_8;
	private CLabel label_10;
	private String jfid;
	/**
	 * @wbp.nonvisual location=33,542
	 */
	private final Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public HyjfhkDialog(Shell parent, String jfid) {
		super(parent, SWT.NONE);
		this.jfid = jfid;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		dataset.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(batch);
		batchItem
				.setCommand("update  hyjf set qk=qk - ? ,yh=yh+?,bz=bz||?\r\nwhere id=?");
		batchItem.setName("修改消费记录");
		shell = new Shell(getParent(), SWT.DIALOG_TRIM);
		shell.setSize(470, 304);
		shell.setText("会员交费补收欠款");
		shell.setLayout(new GridLayout(1, false));

		Group group = new Group(shell, SWT.NONE);
		GridLayout gl_group = new GridLayout(6, false);
		gl_group.marginHeight = 10;
		gl_group.verticalSpacing = 20;
		gl_group.marginWidth = 10;
		gl_group.horizontalSpacing = 0;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("会员信息");

		Label label = new Label(group, SWT.NONE);
		label.setText("会员编号：");

		lblNewLabel = new CLabel(group, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true,
				false, 1, 1));

		Label label_1 = new Label(group, SWT.NONE);
		label_1.setText("会员姓名：");

		label_2 = new CLabel(group, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));

		Label label_3 = new Label(group, SWT.NONE);
		label_3.setText("交费日期：");

		label_5 = new CLabel(group, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label_4 = new Label(group, SWT.NONE);
		label_4.setText("交费类型：");

		label_6 = new CLabel(group, SWT.NONE);
		label_6.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label_7 = new Label(group, SWT.NONE);
		label_7.setText("应收金额：");

		label_8 = new CLabel(group, SWT.NONE);
		label_8.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_8.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Label label_9 = new Label(group, SWT.NONE);
		label_9.setText("欠款金额：");

		label_10 = new CLabel(group, SWT.NONE);
		label_10.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_10.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Group group_1 = new Group(shell, SWT.NONE);
		GridLayout gl_group_1 = new GridLayout(4, false);
		gl_group_1.marginHeight = 10;
		gl_group_1.verticalSpacing = 20;
		gl_group_1.marginWidth = 10;
		group_1.setLayout(gl_group_1);
		GridData gd_group_1 = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_group_1.verticalIndent = 10;
		group_1.setLayoutData(gd_group_1);
		group_1.setText("收款信息");

		Label label_11 = new Label(group_1, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_11.setText("还款金额：");

		text = new Text(group_1, SWT.BORDER);
		text.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		text.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 100;
		text.setLayoutData(gd_text);
		ControlUtil.setCurrencyRule(text, false, false);
		ControlUtil.setEnterToFocusMove(text);

		Label label_13 = new Label(group_1, SWT.NONE);
		GridData gd_label_13 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_label_13.horizontalIndent = 50;
		label_13.setLayoutData(gd_label_13);
		label_13.setText("优惠金额：");

		text_1 = new Text(group_1, SWT.BORDER);
		text_1.setText("0");
		text_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		text_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_text_1.widthHint = 100;
		text_1.setLayoutData(gd_text_1);
		ControlUtil.setCurrencyRule(text_1, false, false);
		ControlUtil.setEnterToFocusMove(text_1);
		Label label_12 = new Label(group_1, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_12.setText("还款备注：");

		text_2 = new Text(group_1, SWT.BORDER);
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				3, 1));
		ControlUtil.setEnterToFocusMove(text_2);

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_composite.verticalIndent = 10;
		composite.setLayoutData(gd_composite);

		Button btnCheckButton = new Button(composite, SWT.CHECK);
		btnCheckButton.setText("收款后打印收款单据");
		ControlUtil.setEnterToFocusMove(btnCheckButton);
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setSelection(true);
		GridData gd_btnNewButton = new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1);
		gd_btnNewButton.widthHint = 80;
		gd_btnNewButton.heightHint = 25;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("确定（F5）");
		btnNewButton.setFocus();
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.heightHint = 25;
		gd_btnNewButton_1.widthHint = 80;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("取消（ESC）");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				shell.close();
			}
		});
		setParams();
		createGUIAfter();
		setValues();

	}

	protected void doSave() {
		setParams();
		try {
			batch.execute();
			result = SWT.OK;
			shell.close();
		} catch (Exception e) {
			MessageDialog.openError(shell, "异常", "还款失败:" + e.getMessage());
		}
	}

	protected void setParams() {
		dataset.setParam(0, jfid);
		batch.setParam(0, 0, text.getText());
		batch.setParam(0, 1, text_1.getText());
		String bz = SimpleDateFormat.getInstance().format(new Date());
		bz = "[" + bz + "还款备注:" + text_2.getText() + "]";
		batch.setParam(0, 2, bz);
		batch.setParam(0, 3, dataset.getValue("id"));
	}

	protected void setValues() {
		lblNewLabel.setText(dataset.getValue("hybh", String.class, ""));
		label_2.setText(dataset.getValue("hyxm", String.class, ""));
		label_5.setText(dataset.getValue("rq", String.class, ""));
		label_6.setText(dataset.getValue("ztsm", String.class, ""));
		label_8.setText(dataset.getValue("yfje", String.class, ""));
		label_10.setText(dataset.getValue("qk", String.class, ""));
		text.setText(dataset.getValue("qk", String.class, ""));
	}

	protected void createGUIAfter() {
		dataset.update();
		dataset.setSelected(0);
	}
}
